package userInterface;
import dataProces.DatabaseOperations;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class Transfer extends JDialog implements ActionListener {
    JPanel[] jPanels = new JPanel[5];
    JTextField[] jFields = new JTextField[4];
    JButton[] jButtons = new JButton[4];
    String[] prompt = { "目标账号", "目标查找", "转账金额", "查看余额", "转账", "返回", "转账界面" };

    public Transfer() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(6, 1));
        for (int i = 0; i < jPanels.length; i++) {
            jPanels[i] = new JPanel();
            if (i < 4) {
                jFields[i] = new JTextField(15);
                if (i % 2 == 0) {
                    jPanels[i].add(new JLabel(prompt[i]));
                } else {
                    jButtons[i / 2] = new JButton(prompt[i]);
                    jButtons[i / 2].addActionListener(this);
                    jPanels[i].add(jButtons[i / 2]);
                    jFields[i].setEditable(false);
                }
                jPanels[i].add(jFields[i]);
            } else
                for (int j = 2; j < jButtons.length; j++) {
                    jButtons[j] = new JButton(prompt[i + j - 2]);
                    jButtons[j].addActionListener(this);
                    jPanels[i].add(jButtons[j]);
                }
            container.add(jPanels[i]);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[6]);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        DatabaseOperations dOperations = new DatabaseOperations();
        if (e.getSource() == jButtons[0]) {
            String name = dOperations.dataQueries("user", jFields[0].getText().trim(), "name");
            if (name != null)
                jFields[1].setText(name);
            else
                JOptionPane.showMessageDialog(null, "查无此人！");
        } else if (e.getSource() == jButtons[1]) {
            jFields[3].setText(dOperations.dataQueries("investment", Login.user, "money"));
        } else if (e.getSource() == jButtons[2]) {
            if (!Objects.equals(jFields[2].getText(), Login.user)) {
                double money = Double.parseDouble(dOperations.dataQueries("investment", Login.user, "money"));
                double fund = Double.parseDouble(jFields[2].getText().trim());
                if (money < fund)
                    JOptionPane.showMessageDialog(null, "账户余额不足！");
                else {
                    money -= fund;
                    double airMoney = Double
                            .parseDouble(dOperations.dataQueries("investment", jFields[0].getText(), "money"));
                    airMoney += fund;
                    dOperations.dataUpdates("investment", Login.user, "money", String.format("%.2f", money));
                    dOperations.dataUpdates("investment", jFields[0].getText().trim(), "money",
                            String.format("%.2f", airMoney));
                    JOptionPane.showMessageDialog(null, "已向"
                            + dOperations.dataQueries("user", jFields[0].getText().trim(), "name") + "转账" + fund + "元");
                    dispose();
                    new Transfer();
                }
            }else JOptionPane.showMessageDialog(null,"不能向自己转账哦！");
        } else if (e.getSource() == jButtons[3]) {
            dispose();
            new UserMenu();
        }
    }

    public static void main(String[] args) {
        new Transfer();
    }
}
